package com.example.animations.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.animations.R;
import com.example.animations.models.MainMenuItem;

import java.util.List;

public class MainListAdapter extends BaseAdapter {
    private final Context _context;
    private final List<MainMenuItem> _items;

    public MainListAdapter(Context context, List<MainMenuItem> items) {
        super();
        _context = context;
        _items = items;
    }

    @Override
    public int getCount() {
        return _items.size();
    }

    @Override
    public Object getItem(int i) {
        return _items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int viewID = getItemViewType(i) == 0 ? R.layout.main_list_section_header : R.layout.main_list_item;

        if (view == null) {
            view = LayoutInflater.from(_context).inflate(viewID, viewGroup, false);
        }
        TextView title = view.findViewById(R.id.title_text);
        title.setText(((MainMenuItem) getItem(i)).getTitle());

        return view;
    }

    @Override
    public int getItemViewType(int position) {
        MainMenuItem item = _items.get(position);
        return item.getActionType() == MainMenuItem.MainActionType.NONE ? 0 : 1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
}
